package com.zyt.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBitisUtil {
    private static SqlSessionFactory sqlSessionFactory =null;
//    创建链接
    static {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    接受返回值
    public static SqlSession getSession(){
        SqlSession session = sqlSessionFactory.openSession();
        return session;
    }
//      关流
    public static void closeSession( SqlSession session){
        if (session!=null){
            session.close();
        }
    }
}
